package ssm.blog.controller.admin;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.blog.entity.Blog;
import ssm.blog.entity.BlogType;
import ssm.blog.service.BlogService;
import ssm.blog.service.BlogTypeService;
import ssm.blog.util.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.google.common.primitives.Ints.min;

/**
 * @program: lyon
 * @description: 博客-控制层
 * @author: Lyon
 * @create: 2018-05-30 17:20
 **/
@Controller
@RequestMapping("/admin")
public class AdminBlogController {
    @Resource
    BlogTypeService blogTypeService;
    @Resource
    BlogService blogService;
    /**
    * @Description:  添加博客-视图
    * @Param: [model]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/31
    */
    @RequestMapping("/blogAdd")
    String blogAdd(Model model){
        List<BlogType>blogTypes=blogTypeService.list();
        model.addAttribute("blogTypes",blogTypes);
        return "interface/admin/blogAdd";
    }
    /**
    * @Description:  添加博客
    * @Param: [blog, response]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/31
    */
    @RequestMapping("/addBlog")
    String addBlog(Blog blog, HttpServletResponse response)throws Exception{
        int resultRows=blogService.add(blog);
        //更新改分类下的最新提交时间
        BlogType blogType=blogTypeService.selectById(blog.getType_id());
        blogType.setLast_commit_date(blog.getReleas_date());
        blogType.setBlog_count(blogType.getBlog_count()+1);
        blogTypeService.updateByIdSelective(blogType);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
     * @Description:  单个删除博客
     * @Param: [response, id]
     * @return: java.lang.String
     * @Author: Lyon
     * @Date: 2018/5/30
     */
    @RequestMapping("/blogDele")
    String blogDele(HttpServletResponse response,Integer id,Integer type_id) throws Exception {
        int resultRows=blogService.deleById(id);
        BlogType blogType=blogTypeService.selectById(type_id);
        blogType.setBlog_count(blogType.getBlog_count()-1);
        blogTypeService.updateByIdSelective(blogType);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else {
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
     * @Description:  批量删除博客
     * @Param: [response, ids]
     * @return: java.lang.String
     * @Author: Lyon
     * @Date: 2018/5/30
     */
    @RequestMapping("/blogsDele")
    String blogsDele(HttpServletResponse response,String ids)throws Exception{
        //分割得到所有ID
        String[] IDS_str=ids.split(",");
        //转化为Integer数组
        List<Integer> IDS_INT=new ArrayList<>();
        List<Integer> TYPEIDS_INT=new ArrayList<>();
        for(String i:IDS_str) {
            String[] idAndType_id=i.split("&");
            IDS_INT.add(Integer.valueOf(idAndType_id[0]));
            TYPEIDS_INT.add(Integer.valueOf(idAndType_id[1]));
        }
        boolean flag=true;
        for(int i=0;i<TYPEIDS_INT.size();i++)
        {
            int type_id=TYPEIDS_INT.get(i);
            BlogType blogType=blogTypeService.selectById(type_id);
            blogType.setBlog_count(blogType.getBlog_count()-1);
            int rows=blogTypeService.updateByIdSelective(blogType);
            if(!(rows>0)){
                flag=false;
            }
        }
        //删除
        int resultRows=blogService.deles(IDS_INT);
        //返回删除结果
        JSONObject result=new JSONObject();
        if(resultRows>0&&flag){
            result.put("success",true);
        }else {
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    /**
    * @Description:  编辑博客- 视图
    * @Param: [model, id]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/31
    */
    @RequestMapping("/blogEdit")
    String blogEdit(Model model,Integer id){
        Blog blog=blogService.selectById(id);
        model.addAttribute("blog",blog);
        List<BlogType>blogTypes=blogTypeService.list();
        model.addAttribute("blogTypes",blogTypes);
        return "interface/admin/blogEdit";
    }
    @RequestMapping("/getBlogJson")
    String getBlogJson(Integer id,HttpServletResponse response) throws Exception {
        Blog blog=blogService.selectById(id);
        String jsonStr=JSONObject.toJSONString(blog);
        JSONObject result=JSONObject.parseObject(jsonStr);
        ResponseUtil.write(response,result);
        return null;
    }
    /**
    * @Description:  更新博客
    * @Param: [blog, response]
    * @return: java.lang.String
    * @Author: Lyon
    * @Date: 2018/5/31
    */
    @RequestMapping("/updateBlog")
    String updateBlog(Blog blog,HttpServletResponse response,Integer old_type_id)throws Exception{
        JSONObject result=new JSONObject();
        int resultRows=blogService.updateById(blog);
        if(!blog.getType_id().equals(old_type_id)){
            BlogType old_blogType=blogTypeService.selectById(old_type_id);
            old_blogType.setBlog_count(old_blogType.getBlog_count()-1);
            resultRows=min(blogTypeService.updateByIdSelective(old_blogType),resultRows);

            BlogType blogType=blogTypeService.selectById(blog.getType_id());
            blogType.setBlog_count(blogType.getBlog_count()+1);
            resultRows=min(blogTypeService.updateByIdSelective(blogType),resultRows);
        }
        if(resultRows>0){
            result.put("success",true);
        }else {
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
}
